<?php

namespace Database\Factories;

use App\Models\RedemptionCode;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\RedemptionCode>
 */
class RedemptionCodeFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        $types = [
            RedemptionCode::TYPE_TIME_ACCESS,
            RedemptionCode::TYPE_DISCOUNT,
            RedemptionCode::TYPE_PRODUCT_EXCHANGE
        ];
        
        $type = $this->faker->randomElement($types);
        $status = $this->faker->randomElement([
            RedemptionCode::STATUS_UNUSED,
            RedemptionCode::STATUS_USED,
            RedemptionCode::STATUS_EXPIRED,
            RedemptionCode::STATUS_DISABLED
        ]);
        
        // 根据类型生成不同的metadata
        $metadata = [];
        switch ($type) {
            case RedemptionCode::TYPE_TIME_ACCESS:
                $metadata = [
                    'days' => $this->faker->numberBetween(7, 365),
                    'permission_level' => $this->faker->randomElement(['premium', 'vip', 'special_access'])
                ];
                break;
            case RedemptionCode::TYPE_DISCOUNT:
                $amount = $this->faker->randomFloat(2, 10, 500);
                $metadata = [
                    'amount' => $amount,
                    'currency' => 'CNY'
                ];
                // 30%的概率添加最大折扣限制
                if ($this->faker->boolean(30)) {
                    $metadata['max_discount'] = min($amount * $this->faker->randomFloat(2, 0.5, 1.5), 500);
                }
                // 40%的概率添加适用商品限制
                if ($this->faker->boolean(40)) {
                    $metadata['applicable_products'] = $this->faker->randomElements(range(1001, 1050), $this->faker->numberBetween(1, 5));
                }
                break;
            case RedemptionCode::TYPE_PRODUCT_EXCHANGE:
                $metadata = [
                    'product_id' => $this->faker->numberBetween(2001, 2100),
                    'product_name' => $this->faker->productName,
                    'quantity' => 1
                ];
                break;
        }
        
        $expired = $this->faker->boolean(70); // 70%的码有过期时间
        $used = $status === RedemptionCode::STATUS_USED;
        
        return [
            'code' => RedemptionCode::generateUniqueCode(),
            'type' => $type,
            'status' => $status,
            'expired_at' => $expired ? $this->faker->dateTimeBetween('now', '+365 days') : null,
            'used_at' => $used ? $this->faker->dateTimeBetween('-30 days', 'now') : null,
            'used_user_id' => $used ? $this->faker->randomNumber(5) : null,
            'used_order_id' => $used ? $this->faker->randomNumber(5) : null,
            'metadata' => $metadata
        ];
    }
    
    /**
     * 配置生成未使用的兑换码
     */
    public function unused()
    {
        return $this->state(function (array $attributes) {
            return [
                'status' => RedemptionCode::STATUS_UNUSED,
                'used_at' => null,
                'used_user_id' => null,
                'used_order_id' => null,
                'expired_at' => $this->faker->dateTimeBetween('now', '+365 days')
            ];
        });
    }
    
    /**
     * 配置生成已使用的兑换码
     */
    public function used()
    {
        return $this->state(function (array $attributes) {
            return [
                'status' => RedemptionCode::STATUS_USED,
                'used_at' => $this->faker->dateTimeBetween('-30 days', 'now'),
                'used_user_id' => $this->faker->randomNumber(5),
                'used_order_id' => $this->faker->randomNumber(5)
            ];
        });
    }
    
    /**
     * 配置生成时间权限类型的兑换码
     */
    public function timeAccess()
    {
        return $this->state(function (array $attributes) {
            return [
                'type' => RedemptionCode::TYPE_TIME_ACCESS,
                'metadata' => [
                    'days' => $this->faker->numberBetween(7, 365),
                    'permission_level' => $this->faker->randomElement(['premium', 'vip', 'special_access'])
                ]
            ];
        });
    }
    
    /**
     * 配置生成折扣类型的兑换码
     */
    public function discount()
    {
        return $this->state(function (array $attributes) {
            $amount = $this->faker->randomFloat(2, 10, 200);
            return [
                'type' => RedemptionCode::TYPE_DISCOUNT,
                'metadata' => [
                    'amount' => $amount,
                    'currency' => 'CNY',
                    'max_discount' => $amount * $this->faker->randomFloat(2, 0.8, 1.2)
                ]
            ];
        });
    }
    
    /**
     * 配置生成商品兑换类型的兑换码
     */
    public function productExchange()
    {
        return $this->state(function (array $attributes) {
            return [
                'type' => RedemptionCode::TYPE_PRODUCT_EXCHANGE,
                'metadata' => [
                    'product_id' => $this->faker->numberBetween(2001, 2100),
                    'product_name' => $this->faker->productName,
                    'quantity' => 1
                ]
            ];
        });
    }
}